package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.QualityGoodsMapper;
import com.applet.applet_shangcheng.entity.Announcement;
import com.applet.applet_shangcheng.entity.QualityGoods;
import com.applet.applet_shangcheng.utlis.DateUtils;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class QualityGoodsService {

    @Autowired
    private QualityGoodsMapper qualityGoodsMapper;


    public int insertQualityGoods(String goodsCode) {
        QualityGoods qualityGoods1 = new QualityGoods();
        qualityGoods1.setGoodsCode(goodsCode);
        qualityGoods1.setCreateDate(new Date());
        qualityGoods1.setNote("可扫码!");
        qualityGoods1.setStatus(1);
        int goods = qualityGoodsMapper.insertQualityGoods(qualityGoods1);
        return goods;
    }

    @Transactional(rollbackFor = Exception.class)
    public QualityGoods findByCode(String goodsCode) {
        QualityGoods qualityGoods = qualityGoodsMapper.findByCode(goodsCode);
        if (qualityGoods != null) {
            qualityGoods.setScanTheCode(new Date());
            qualityGoods.setStatus(1);
            qualityGoods.setNote("验证成功!");
            qualityGoodsMapper.updateQualityGoods(qualityGoods);
        }
        return qualityGoods;
    }

    ;

    public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<QualityGoods> page = qualityGoodsMapper.findAll(queryString);
        for (QualityGoods qualityGoods : page) {
            Date createDate = qualityGoods.getCreateDate();
            Date scanTheCode = qualityGoods.getScanTheCode();
            try {
                String date2String = DateUtils.parseDate2String(createDate);
                qualityGoods.setCreateDate1(date2String);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (scanTheCode != null) {
                try {
                    String date2String = DateUtils.parseDate2String(createDate);
                    qualityGoods.setScanTheCode1(date2String);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        PageInfo<QualityGoods> pageInfo = new PageInfo<>(page);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    public int deleteCode(String id) {
        return qualityGoodsMapper.deleteCode(id);
    }

    public QualityGoods findById(int id) {
        return qualityGoodsMapper.findById(id);
    }

    public int update(QualityGoods qualityGoods) {
        return qualityGoodsMapper.update(qualityGoods);
    }
}
